package database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Database implements DatabaseState {
	
	
	static private DbcpConnection dbcpConnection = new DbcpConnection(
					"jdbc:mysql://127.0.0.1/ifttt", "root", "Soviet");
	
	static public Connection getConnection() {
		try {
			return (Connection) dbcpConnection.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	static public int getLastId() {
		try {
			Connection con = Database.getConnection();
			Statement stmt = con.createStatement();
			
			ResultSet rs = stmt.executeQuery(
					"select last_insert_id() id");
			
			rs.next();
			int id = rs.getInt("id");
			
			stmt.close();
			con.close();
			
			return id;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
}
